//
//  StudyInforVC.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/1/20.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "StudyInforVC.h"
#import "PracticeVC.h"
#import "DataInforVC.h"
#import "WorksManagementVC.h"
#import "studyTeachCell.h"
#import "studyStudyCell.h"
#import "DownLoadVC.h"
#import "studyPracticeView.h"
#import "QuestionModel.h"

@interface StudyInforVC ()<studyTeachCellDelegate>

@property (strong, nonatomic) IBOutlet UIButton *btn1;
@property (strong, nonatomic) IBOutlet UIButton *btn2;
@property (strong, nonatomic) IBOutlet UIButton *btn3;
@property (strong, nonatomic) IBOutlet UIButton *btn4;
@property (strong, nonatomic) IBOutlet UIImageView *line1;
@property (strong, nonatomic) IBOutlet UIImageView *line2;
@property (strong, nonatomic) IBOutlet UIImageView *line3;
@property (strong, nonatomic) IBOutlet UIView *bottom_view;
@property (strong, nonatomic) IBOutlet UIImageView *bottom_img;

@end

@implementation StudyInforVC{
    RequestServices *service;
    NSDictionary *valueDic;//获取到的obj值
    studyTeachCell *teachView;//教 view
    studyStudyCell *studyView;//学 view
    studyPracticeView *practiceView;//练 view
    NSMutableArray *questionArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.name;
    [self.backKuang setHidden:YES];
    self.btn1.titleLabel.font = self.btn2.titleLabel.font = self.btn3.titleLabel.font = self.btn4.titleLabel.font = AutoLabFont1;
    self.bottom_img.image = [UIImage imageNamed:@"back_center"];
    [self.bottom_view insertSubview:self.bottom_img atIndex:-1];
    service = [[RequestServices alloc]initWithDelegate:self];
    questionArr = [NSMutableArray array];
    service.view = self.view;
    [service getStepsLock:self.courseId tag:8000];
}

#pragma mark - requestDelegate
-(void)getFinished:(NSDictionary *)obj tag:(long long)tag{
    [self HideLoading];
    if (tag == 8000) {
        //进程是否加锁
        valueDic = [NSDictionary dictionaryWithDictionary:obj];
        [self showTeachView];
    }
    
    if (tag == 8001) {
        //统计观看视频时长
        NSLog(@"统计观看视频时长");
    }
    
    if (tag == 8002) {
        questionArr = [QuestionModel mj_objectArrayWithKeyValuesArray:obj[@"list"]];
        NSLog(@"练习题库--%@--问题数组%@-",obj,questionArr);
        practiceView.getArr = [NSMutableArray arrayWithArray:questionArr];
        practiceView.chooseCount = questionArr.count;
        [practiceView.collection_view reloadData];
    }
}

-(void)getError:(NSError *)error tag:(long long)tag{
    [self HideLoading];
    [self ShowNewMsg:error.domain];
}

/**
 * 展示教
 */

-(void)showTeachView{
    if (!teachView) {
        teachView = [[[NSBundle mainBundle]loadNibNamed:@"studyTeachCell" owner:self options:nil]lastObject];
        teachView.frame = self.bottom_view.bounds;
        teachView.teachDic = self.passDic;
        teachView.delegate = self;
        [self.bottom_view addSubview:teachView];
    }
}
#pragma mark ------ studyTeachCellDelegate
-(void)upLoadZuoPin{
    //上传作品 0解锁 1锁住
    if ([valueDic[@"production"] intValue] == 1) {
        [self ShowNewMsg:@"当前不可上传作品"];
    }else{
        //上传作品
        DownLoadVC *vc = [[DownLoadVC alloc]init];
        [self PushToViewController:vc];
    }
}

-(void)showCanKaoZiLiao{
    //参考资料
    DataInforVC *vc = [[DataInforVC alloc]init];
    [self PushToViewController:vc];
}
/**
 * 教
 */
- (IBAction)teach:(id)sender {
    [self.btn1 setBackgroundImage:[UIImage imageNamed:@"study_round"] forState:UIControlStateNormal];
    [self.btn2 setBackgroundImage:[UIImage imageNamed:@"study_round_1"] forState:UIControlStateNormal];
    [self.btn3 setBackgroundImage:[UIImage imageNamed:@"study_round_1"] forState:UIControlStateNormal];
    [self.btn4 setBackgroundImage:[UIImage imageNamed:@"study_round_1"] forState:UIControlStateNormal];
    
    self.line1.image = [UIImage imageNamed:@"study_line"];
    self.line2.image = self.line3.image = [UIImage imageNamed:@"study_line_1"];
    
    //展示教 view
    [self showTeachView];
    
    //隐藏学、练、测
    [studyView removeFromSuperview];
     studyView = nil;
    [practiceView removeFromSuperview];
    practiceView = nil;
    [self upLoadwatchTimes];

}


/**
 * 展示学
 */

-(void)showStudyView{
    if (!studyView) {
        studyView = [[[NSBundle mainBundle]loadNibNamed:@"studyStudyCell" owner:self options:nil]lastObject];
        studyView.frame = self.bottom_view.bounds;
        [self.bottom_view addSubview:studyView];
    }
    
    studyView.right_textView.text = [NSString stringWithFormat:@"%@",self.passDic[@"courseDesc"]];
    //步骤1：获取视频路径
//    NSString *webVideoPath = @"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
    NSString *webVideoPath = [NSString stringWithFormat:@"%@",self.passDic[@"video"]];
    NSURL *webVideoUrl = [NSURL URLWithString:webVideoPath];
    //步骤2：创建AVPlayer
    AVPlayer *avPlayer = [[AVPlayer alloc] initWithURL:webVideoUrl];
    //步骤3：使用AVPlayer创建AVPlayerViewController，并跳转播放界面
    AVPlayerViewController *avPlayerVC =[[AVPlayerViewController alloc] init];
    avPlayerVC.player = avPlayer;
    //视频sizeFill
    avPlayerVC.videoGravity = AVLayerVideoGravityResize;
    //设置播放速度
    //        avPlayer.rate=1.0;
    //跳到指定时间点播放
    [avPlayer seekToTime:CMTimeMake(1,1)];
    //步骤4：设置播放器视图大小
    avPlayerVC.view.frame = studyView.left_view.bounds;
    //特别注意:AVPlayerViewController不能作为局部变量被释放，否则无法播放成功
    //解决1.AVPlayerViewController作为属性
    //解决2:使用addChildViewController，AVPlayerViewController作为子视图控制器
    [self addChildViewController:avPlayerVC];
    [studyView.left_view addSubview:avPlayerVC.view];
    __weak AVPlayer *_avPlayer;
    _avPlayer = avPlayer;
    [avPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1,1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        float total1 = _avPlayer.currentItem.duration.value;//帧数
        float total2 = _avPlayer.currentItem.duration.timescale;//每秒帧数
        //总时长
        self.totalTime = total1/total2;
        NSLog(@"--帧数-%.1f--秒帧数-%.1f--总时间%.1f-",total1,total2,self.totalTime);
        //当前时间
        self.watchedTime = time.value * 1.0f/time.timescale;
        NSLog(@"--观看时间--%.1f",self.watchedTime);
    }];
}

//上传观看时间
-(void)upLoadwatchTimes{
    if (self.watchedTime > 1 ) {
        [self HideLoading];
        [service checkWatchVideo:self.courseId current_look_minutes:[NSString stringWithFormat:@"%.0f",self.watchedTime] max_video_minutes:[NSString stringWithFormat:@"%.0f",self.totalTime] tag:8001];
    }
}

/**
*  学
*/
- (IBAction)study:(id)sender {
    [self.btn1 setBackgroundImage:[UIImage imageNamed:@"study_round"] forState:UIControlStateNormal];
    [self.btn2 setBackgroundImage:[UIImage imageNamed:@"study_round"] forState:UIControlStateNormal];
    [self.btn3 setBackgroundImage:[UIImage imageNamed:@"study_round_1"] forState:UIControlStateNormal];
    [self.btn4 setBackgroundImage:[UIImage imageNamed:@"study_round_1"] forState:UIControlStateNormal];
    
    self.line1.image = self.line2.image = [UIImage imageNamed:@"study_line"];
    self.line3.image = [UIImage imageNamed:@"study_line_1"];
    
    //显示学view
    [self showStudyView];

    //隐藏 教 练 测
    [teachView removeFromSuperview];
    teachView = nil;
    [practiceView removeFromSuperview];
    practiceView = nil;
}

/**
*   练
*/
- (IBAction)practice:(id)sender {
    //练 0解锁 1锁住
//    if ([valueDic[@"practice"]intValue] == 1) {
//        [self ShowNewMsg:@"当前不可练习"];
//    }else{
        [self.btn1 setBackgroundImage:[UIImage imageNamed:@"study_round"] forState:UIControlStateNormal];
        [self.btn2 setBackgroundImage:[UIImage imageNamed:@"study_round"] forState:UIControlStateNormal];
        [self.btn3 setBackgroundImage:[UIImage imageNamed:@"study_round"] forState:UIControlStateNormal];
        [self.btn4 setBackgroundImage:[UIImage imageNamed:@"study_round_1"] forState:UIControlStateNormal];
        
        self.line1.image = self.line2.image = self.line3.image = [UIImage imageNamed:@"study_line"];
        
        //显示练view
        [self showPracticeView];
        
        //隐藏 教、学、测
        [teachView removeFromSuperview];
        teachView = nil;
        [studyView removeFromSuperview];
        studyView = nil;
//    }
    [self upLoadwatchTimes];
}
-(void)showPracticeView{
    if (!practiceView) {
        practiceView = [[[NSBundle mainBundle]loadNibNamed:@"studyPracticeView" owner:self options:nil]lastObject];
        practiceView.frame = self.bottom_view.bounds;
        practiceView.vc = self;
        [self.bottom_view addSubview:practiceView];
    }
    //获取练 题库
    [service getQuestionBank:self.courseId current:@(1) size:@(10) type:@(1) tag:8002];
}

/**
*  测
*/
- (IBAction)test:(id)sender {
    //测 0解锁 1锁住
    if ([valueDic[@"test"]intValue] == 1) {
        [self ShowNewMsg:@"当前不可测试"];
    }else{
        [self.btn1 setBackgroundImage:[UIImage imageNamed:@"study_round"] forState:UIControlStateNormal];
        [self.btn2 setBackgroundImage:[UIImage imageNamed:@"study_round"] forState:UIControlStateNormal];
        [self.btn3 setBackgroundImage:[UIImage imageNamed:@"study_round"] forState:UIControlStateNormal];
        [self.btn4 setBackgroundImage:[UIImage imageNamed:@"study_round"] forState:UIControlStateNormal];
        
        self.line1.image = self.line2.image = self.line3.image = [UIImage imageNamed:@"study_line"];
    }
    [self upLoadwatchTimes];

}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:YES];
    [self upLoadwatchTimes];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
